$(function () {
    // 默认加载部门树
    vm.getDept();
});
var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "complexName" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    //此处treeNode 为当前节点
    vm.q.curDeptId = treeObj.deptId;
    // 执行查询
    vm.loadDevList();
}

var myChart;
var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            curDeptId: '', // 所属车间地点ID
            devCode: '', // 设备编码
            paramName: '', // 参数名称
            beginTime: '', // 开始时间
            endTime:'' // 结束时间
        },
        devList:[],  // 设备列表
        devParamList:[], // 设备参数列表
        orderFields:[
            {name:'报警频次TOP显示',value:'alarm'},{name:'维修频次TOP显示',value:'repair'}
        ],
        option: {
            title: {
                text: '设备运行参数分析图',
                left: 'center',
                subtext: '请选择设备和参数执行查询'
            },
            tooltip: {
                trigger: 'axis'
            },
            xAxis: {
                data: []
            },
            yAxis: {
                splitLine: {
                    show: false
                }
            },
            dataZoom: [{
                startValue: '2014-06-01'
            }, {
                type: 'inside'
            }],
            visualMap: {
                top: 10,
                right: 10,
                pieces: [], // 由服务器端传入
                outOfRange: {
                    color: '#999'
                }
            },
            series: [{
                name: '运行',
                type: 'line',
                data: [],
                markLine: {
                    silent: true,
                    data: [] // 由服务器端返回
                }
            }]
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCX',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    var node = ztree.getNodeByParam("deptId", '01');
                    if (node) {
                        vm.q.curDeptId = node.deptId;
                        ztree.selectNode(node);
                        vm.loadDevList();
                        ztree.expandNode(node)
                    }else{
                        ztree.expandAll(true);
                    }
                }
            });
        },
        search: function () {
            var param = {};
            if(vm.q.devCode){
                param.devCode = vm.q.devCode;
            }
            if(vm.q.paramName){
                param.paramName = vm.q.paramName;
            }
            if(vm.q.beginTime){
                param.beginTime = gmt2DateStr(vm.q.beginTime);
            }
            if(vm.q.endTime){
                param.endTime =  gmt2DateStr(vm.q.endTime);
            }
            myChart.showLoading();
            Ajax.request({
                url: "../device/stat/paramLineData",
                params: JSON.stringify(param),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    myChart.hideLoading();
                    var paramLineData = res.paramLineData;
                    if(paramLineData.optionData.length == 0){
                        vm.option.title.subtext = '该设备下暂未采集[' + vm.q.paramName + ']参数';
                    }else{
                        vm.option.title.subtext = '';
                    }
                    vm.option.visualMap.pieces = paramLineData.visualMapPieces;
                    vm.option.xAxis.data=paramLineData.optionData.map(function (item) {return item[0]});
                    vm.option.series[0].data=paramLineData.optionData.map(function (item) {return item[1]});
                    vm.option.series[0].markLine.data=paramLineData.seriesMarkLineData;
                    myChart.setOption(vm.option, true);
                }
            });
        },
        reset: function(){
            vm.q = {
                curDeptId: '', // 所属车间地点ID
                devCode: '', // 设备编码
                paramName: '', // 参数名称
                beginTime: '', // 开始时间
                endTime:'' // 结束时间
            };
        },
        loadDevList: function(){
            vm.devParamList = [];
            // 加载组织下的设备列表
            Ajax.request({
                url: "../device/stat/devList?deptId=" + vm.q.curDeptId,
                type: "GET",
                contentType: "application/json",
                successCallback: function (res) {
                    vm.devList = res.devList;
                    if(res.devList.length > 0){
                        vm.q.devCode = res.devList[0].DEV_CODE;
                        vm.loadDevParamList();
                    }
                }
            });

        },
        loadDevParamList: function(){
            Ajax.request({
                url: "../device/stat/devParamList?devCode=" + vm.q.devCode,
                type: "GET",
                contentType: "application/json",
                successCallback: function (res) {
                    vm.devParamList = res.devParamList;
                    vm.q.paramName = '';

                }
            });
        }
    },
    mounted: function(){
        // 初始化部门树
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option,true)  //setOption 用this.option中的数据开始作图
    }
});
